<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
/**
*
* @version $Id: ps_alphauserpoints.php 10 2010-10-07 14:59:07Z ahmtsari $

* @copyright Copyright (C) 2004-2008 Adrien Roussel - All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
* http://www.alphaplug.com
* http://www.nordmograph.com
*/



/**
*
* The ps_payment class, containing the default payment processing code
* for payment methods that have no own class
*
*/
class ps_alphauserpoints {

    var $classname = "ps_alphauserpoints";
	var $payment_code = "AUP";
  
    /**
    * Show all configuration parameters for this payment method
    * @returns boolean False when the Payment method has no configration
    */
    function show_configuration() {
        global $VM_LANG;
        $db = new ps_DB();
        
        /** Read current Configuration ***/
        include_once(CLASSPATH ."payment/".$this->classname.".cfg.php");
    ?>
    <table class="adminform">
		<tr class="row0">
        <td><strong>AUP/Currency Ratio </strong></td>
            <td>
               <input type="text" name="AUP_RATIO" class="inputbox" value="<?php  echo AUP_RATIO ?>" />
                    </select>
            </td>
            <td>This ratio defines how many AUP points for one of your Shop's currency unit. Default is 1.
            </td>
        </tr>
      </table>
    <?php
    }
    
    function has_configuration() {
      // return false if there's no configuration
      return true;
   }
   
  /**
	* Returns the "is_writeable" status of the configuration file
	* @param void
	* @returns boolean True when the configuration file is writeable, false when not
	*/
   function configfile_writeable() {
      return is_writeable( CLASSPATH."payment/".$this->classname.".cfg.php" );
   }
   
  /**
	* Returns the "is_readable" status of the configuration file
	* @param void
	* @returns boolean True when the configuration file is writeable, false when not
	*/
   function configfile_readable() {
      return is_readable( CLASSPATH."payment/".$this->classname.".cfg.php" );
   }
   
  /**
	* Writes the configuration file for this payment method
	* @param array An array of objects
	* @returns boolean True when writing was successful
	*/
   function write_configuration( &$d ) {
      
      $my_config_array = array(
                              "AUP_RATIO" => $d['AUP_RATIO']
                            );
      $config = "<?php\n";
      $config .= "if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); \n\n";
      foreach( $my_config_array as $key => $value ) {
        $config .= "define ('$key', '$value');\n";
      }
      
      $config .= "?>";
  
      if ($fp = fopen(CLASSPATH ."payment/".$this->classname.".cfg.php", "w")) {
          fputs($fp, $config, strlen($config));
          fclose ($fp);
          return true;
     }
     else
        return false;
   }
   
  /**************************************************************************
  ** name: process_payment()
  ** returns: 
  ***************************************************************************/
function process_payment($order_number, $order_total, &$d) {
global $vmLogger,$database;   
$auth = $_SESSION['auth'];
$points =0;

/** Read current Ratio Configuration ***/
include_once(CLASSPATH ."payment/".$this->classname.".cfg.php");
$order_total = $order_total * AUP_RATIO ;

$query = "SELECT points FROM #__alpha_userpoints WHERE userid=".$auth["user_id"];
$database->setQuery( $query );
$points = $database->loadResult();

if ($order_total > $points)
		{
$vmLogger->err( "You do not have enough points (only ".$points.") for your order total amount (".$order_total.")");
return false;

		} else 
		
		{

		
$api_AUP = JPATH_SITE.DS.'components'.DS.'com_alphauserpoints'.DS.'helper.php';
if ( file_exists($api_AUP))
			{
    require_once ($api_AUP);
    AlphaUserPointsHelper::newpoints( 'plgaup_vm_purchase', '', '', 'Order#: '.$order_number, -$order_total);
			}   	
						return true;

		}
		 
	}   
}